package com.wb.mas.data.source.http;

import com.wb.mas.BuildConfig;
import com.wb.mas.data.source.HttpDataSource;
import com.wb.mas.data.source.http.service.ApiService;
import com.wb.mas.entity.ApproveResponse;
import com.wb.mas.entity.AuthBackResponse;
import com.wb.mas.entity.BackStatusResponse;
import com.wb.mas.entity.BankResponse;
import com.wb.mas.entity.BannerResponse;
import com.wb.mas.entity.CalculateRateResponse;
import com.wb.mas.entity.DeferInfoResponse;
import com.wb.mas.entity.DicInfoResponse;
import com.wb.mas.entity.FaceCompareResponse;
import com.wb.mas.entity.HelpResponse;
import com.wb.mas.entity.IdentityInfoResponse;
import com.wb.mas.entity.LoginResponse;
import com.wb.mas.entity.LoginTypeResponse;
import com.wb.mas.entity.OrderDetailResponse;
import com.wb.mas.entity.OrderListResponse;
import com.wb.mas.entity.OrderPageResponse;
import com.wb.mas.entity.OrderResponse;
import com.wb.mas.entity.PayCodeResponse;
import com.wb.mas.entity.ProDetailResponse;
import com.wb.mas.entity.RemindInfoResponse;
import com.wb.mas.entity.RootApiBean;
import com.wb.mas.entity.SubmitedAuthResponse;
import com.wb.mas.entity.UploadImageResponse;
import com.wb.mas.entity.VersionResponse;
import com.wb.mas.utils.ParamsUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by goldze on 2019/3/26.
 */
public class HttpDataSourceImpl implements HttpDataSource {
    private ApiService apiService;
    private volatile static HttpDataSourceImpl INSTANCE = null;

    public static HttpDataSourceImpl getInstance(ApiService apiService) {
        if (INSTANCE == null) {
            synchronized (HttpDataSourceImpl.class) {
                if (INSTANCE == null) {
                    INSTANCE = new HttpDataSourceImpl(apiService);
                }
            }
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }

    private HttpDataSourceImpl(ApiService apiService) {
        this.apiService = apiService;
    }

    public Observable<LoginResponse> login(Map<String, Object> params) {
        return apiService.login(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<BannerResponse> getBanner(Map<String, Object> params) {
        return apiService.getBanner(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<ProDetailResponse> getProdDeailList(Map<String, Object> params) {
        return apiService.getProdDeailList(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<CalculateRateResponse> calculateRate(Map<String, Object> params) {
        return apiService.calculateRate(ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<RootApiBean> checkOrderQualifications(Map<String, Object> params) {
        return apiService.checkOrderQualifications(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<ApproveResponse> getApproveStatus(Map<String, Object> params) {
        return apiService.getApproveStatus(ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<RootApiBean> createOrder(Map<String, Object> params) {
        return apiService.createOrder(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<RootApiBean> uploadPhone(Map<String, Object> params, File file) {
        MultipartBody.Part body = MultipartBody.Part.createFormData("userDataFile", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
        return apiService.uploadPhone(BuildConfig.channel, ParamsUtils.getFileBody(params), body);
    }

    @Override
    public Observable<RootApiBean> saveLngLat(Map<String, Object> params) {
        return apiService.saveLngLat(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<UploadImageResponse> uploadImage(Map<String, Object> params, File file) {
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
//        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName());
        Map<String, Object> map = new HashMap<>();
        map.put("paramStr", ParamsUtils.getJsonParams(params));
        return apiService.uploadImage(map, body);
    }

    @Override
    public Observable<FaceCompareResponse> faceCompare(Map<String, Object> params) {
        return apiService.faceCompare(ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<RootApiBean> updateOrSaveUser(Map<String, Object> params) {
        return apiService.updateOrSaveUser(ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<DicInfoResponse> getDicInfo(Map<String, Object> params) {
        return apiService.getDicInfo(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<RootApiBean> saveOrUpdateCustomerInfo(Map<String, Object> params) {
        return apiService.saveOrUpdateCustomerInfo(ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<RootApiBean> saveOrUpdateContactsInfo(Map<String, Object> params) {
        return apiService.saveOrUpdateContactsInfo(ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<IdentityInfoResponse> getIdentityInfo(Map<String, Object> params) {
        return apiService.getIdentityInfo(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<RootApiBean> saveOrUpdateBankList(Map<String, Object> params) {
        return apiService.saveOrUpdateBankList(ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<RootApiBean> imageInformation(Map<String, Object> params) {
        return apiService.imageInformation(ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<OrderResponse> getOrderInfoList(Map<String, Object> params) {
        return apiService.getOrderInfoList(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<OrderPageResponse> getOrderInfoPage(Map<String, Object> params) {
        return apiService.getOrderInfoPage(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<BankResponse> getBankDetail(Map<String, Object> params) {
        return apiService.getBankDetail(ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<PayCodeResponse> getPayCode(Map<String, Object> params) {
        return apiService.getPayCode(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<RootApiBean> rolloverApp(Map<String, Object> params) {
        return apiService.rolloverApp(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<DeferInfoResponse> getDeferInfo(Map<String, Object> params) {
        return apiService.getDeferInfo(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<PayCodeResponse> getDeferPayCode(Map<String, Object> params) {
        return apiService.getDeferPayCode(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<BackStatusResponse> getBackStatus(Map<String, Object> params) {
        return apiService.getBackStatus(ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<HelpResponse> getHelpDatas(Map<String, Object> params) {
        return apiService.getHelpDatas(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<LoginResponse> loginByUserNameAndPassword(Map<String, Object> params) {

        return apiService.loginByUserNameAndPasssword(ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<SubmitedAuthResponse> getSubmitedAuthInfo(Map<String, Object> params) {
        return apiService.getSubmitedAuthInfo(ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<OrderListResponse> getOrderPageList(Map<String, Object> params) {
        return apiService.getOrderPageList(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<OrderDetailResponse> getOrderDetail(Map<String, Object> params) {
        return apiService.getOrderDetail(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<RootApiBean> getSmsCode(Map<String, Object> params) {
        return apiService.getSmsCode(ParamsUtils.getParamsBody(params));

    }

    @Override
    public Observable<LoginResponse> loginByPhoneCode(Map<String, Object> params) {
        return apiService.loginByPhoneCode(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<LoginTypeResponse> getLoginType(Map<String, Object> params) {
        return apiService.getLoginType(BuildConfig.channel, ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<AuthBackResponse> getSubmitedBankInfo(Map<String, Object> params) {
        return apiService.getSubmitedBankInfo( ParamsUtils.getParamsBody(params));
    }

    @Override
    public Observable<RemindInfoResponse> getRemindInfo(Map<String, Object> params) {
        return apiService.getRemindInfo(ParamsUtils.getParamsBody(params));
    }
}
